﻿using Device.Domain;
using Device.Infrstructre.Interface;
using Device.Read.API.Applcation.Command.CarriersCommand;
using MediatR;

namespace Device.Read.API.Applcation.CommandHandlers.CarriersCommandHandlers
{
    //显示承运商中介者
    public class SelectCarriersModelCommandhandler : IRequestHandler<SelectCarriersModelCommand, page>
    {
        private readonly ICarrierRepository _carrierRepository;
        //构造函数
        public SelectCarriersModelCommandhandler(ICarrierRepository carrierRepository)
        {
            _carrierRepository = carrierRepository;
        }

        public async Task<page> Handle(SelectCarriersModelCommand request, CancellationToken cancellationToken)
        {
            page page = new page();
            var res = (await _carrierRepository.GetAllAsync()).Where(x => x.Office_Id == request.Office_Id && x.IsDel == false);
            //承运商名称模糊差
            if (!string.IsNullOrEmpty(request.CarriersName))
            {
                res = res.Where(x => x.CarriersName.Contains(request.CarriersName)).ToList();
            }
            //承运商编号查询
            if (!string.IsNullOrEmpty(request.CarriersCode))
            {
                res = res.Where(x => x.CarriersCode == request.CarriersCode).ToList();
            }
            page.sumcount = res.Count();
            page.lastpage = (int)Math.Ceiling(page.sumcount * 1.0 / request.pagesize);
            page.list = res.Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
            return page;
        }
    }
}
